package com.jiayi.weiphoto.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Environment;

public class DImge {
	public static final String PATH = Environment.getDataDirectory()
			.getAbsolutePath() + "/data/" + "com.jiayi.weiphoto/files/";
	
//	public static final String PATH="/sdcard/yiayi";

	private ExecutorService executorService;
	MapResult mapR = null;
	private DatasLoader dataLoader = null;

	MediaPlayer player = null;
	Context context;

	public DImge(Context context) {
		executorService = Executors.newFixedThreadPool(5);
		this.context=context;
		File file = null;
		file = new File(PATH);
		if (!file.exists()) {
			file.mkdir();
		}
	}

	public void DisplayData(Bitmap bt, String f) {
		dataLoader = new DatasLoader(bt, f);
		executorService.submit(dataLoader);
	}

	class DatasLoader implements Runnable {

		String f = "";
		Bitmap bt;

		public DatasLoader(Bitmap bt, String f) {
			this.f = f;
			this.bt = bt;
		}

		@Override
		public void run() {
			try {
				saveBitmap(bt, f);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public void saveBitmap(Bitmap bitmap, String str) throws IOException {
		File f = new File(PATH + str);
		if (!f.exists()) {
			FileOutputStream out;
			try {
//				out=context.openFileOutput(str, Context.MODE_WORLD_READABLE);
				out = new FileOutputStream(f);
				if (bitmap.compress(Bitmap.CompressFormat.PNG, 70, out)) {
					out.flush();
					out.close();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
